#include "scene.h"
#include <gl/glu.h>
#include "gl/glut.h"
#include <cmath>

namespace Scene
{

mesh_t	lamp, 
					monitor, screen, 
					chair_back, chair_seat;

GLuint				table_side, table_top, table_leg, 
					teapot,
					shelf;

matrix44			matrix_lamp, 
					matrix_monitor, 
					matrix_chair, 
					matrix_teapot,
					matrix_table, 
					matrix_shelf,
					matrix_lamp_view, matrix_lamp_projection;

const float			light_near = 0.35f, light_far = 2.2f;	
const float			table_h = 1.0f;
const float			table_r = 1.5f;
const float			table_top_h = 0.1f;
const vector3		table_pos(.5f,.5f,-1.96f);

vector4				camera_pos;
matrix44			matView, matProj;
int					w, h;
bool				mirror = false;

void init()
{
	GLUquadricObj *quadric = gluNewQuadric();
	gluQuadricDrawStyle(quadric, GLU_FILL);
	gluQuadricTexture(quadric, GL_TRUE);
	gluQuadricNormals(quadric, GLU_SMOOTH);

	table_side = glGenLists(1);
	glNewList(table_side, GL_COMPILE);
	glPushMatrix();
	glTranslatef(.0f,.0f,table_h-table_top_h);
	gluCylinder(quadric, table_r, table_r, table_top_h, 16, 1);
	glPopMatrix();
	glEndList();

	table_top = glGenLists(1);
	glNewList(table_top, GL_COMPILE);
	glPushMatrix();
	glTranslatef(.0f,.0f,table_h);
	gluDisk(quadric, .0f, table_r, 16, 10);
	glPopMatrix();
	glEndList();

	table_leg = glGenLists(1);
	glNewList(table_leg, GL_COMPILE);
	glPushMatrix();
	glRotatef(45.0f,.0f,.0f,1.0f);
	glTranslatef(.0f,-table_r+0.35f,.0f);
	gluCylinder(quadric, 0.1f, 0.1f, table_h-table_top_h, 9, 4);
	glPopMatrix();
	glEndList();

	teapot = glGenLists(1);
	glNewList(teapot, GL_COMPILE);
	glCullFace(GL_FRONT);
	glutSolidTeapot(0.3f);
	glCullFace(GL_BACK);
	glEndList();

	shelf = glGenLists(1);
	glNewList(shelf, GL_COMPILE);
	glutSolidCube(1.0f);
	glEndList();

	monitor.fromFile("media/monitor.mesh");
	screen.fromFile("media/screen.mesh");
	chair_back.fromFile("media/chair_back.mesh");
	chair_seat.fromFile("media/chair_seat.mesh");
	lamp.fromFile("media/lamp.mesh");

	matrix_table = TranslateMatrix44(table_pos.x, table_pos.y, table_pos.z);
	matrix_shelf = TranslateMatrix44(-1.4f,-.6f,-1.46f);
	matrix_teapot = TranslateMatrix44(-1.3f,-.6f,-0.74f)*RotateRadMatrix44('z',DegToRad(90.0f))*RotateRadMatrix44('x',DegToRad(90.0f));
	matrix_monitor = TranslateMatrix44(table_pos.x, table_pos.y, table_pos.z+table_h+0.42f)*RotateRadMatrix44('z',DegToRad(180.0f))*RotateRadMatrix44('x',DegToRad(90.0f));
	matrix_chair = TranslateMatrix44(0.1f, -1.3f, -1.06f)*RotateRadMatrix44('z',DegToRad(-20.0f))*RotateRadMatrix44('x',DegToRad(90.0f));
	update_lamp(.0f);

}

void done()
{
	glDeleteLists(table_side, 1);
	glDeleteLists(table_top, 1);
	glDeleteLists(table_leg, 1);
	glDeleteLists(teapot, 1);
	glDeleteLists(shelf, 1);
}

void update_lamp(float time)
{
	float swing		= 0.3f * sinf(6.28f/8000*(time));
	float rot		= 6.28f/20000*(time);
	matrix_lamp = 
		TranslateMatrix44(.0f,.0f,1.6f) * RotateRadMatrix44('x',DegToRad(90.0f)) * 
		TranslateMatrix44(.0f,0.4f,.0f) * RotateRadMatrix44('y',rot) * RotateRadMatrix44('x',swing) * TranslateMatrix44(.0f,-0.4f,.0f);
	vector4 light_center = matrix_lamp * vector4(.0f,-0.05f,.0f,1.0f);
	vector4 light_target = matrix_lamp * vector4(.0f,-10.0f,.0f,1.0f);
	matrix_lamp_projection = PerspectiveMatrix44(40.0f, 1.0f, light_near, light_far);
	matrix_lamp_view = 
		LookAtMatrix44( vector3(light_center), vector3(.0f,1.0f,.0f), vector3(light_target) );
}

}